<?php declare(strict_types=1);

namespace App\Core;


use Hyperf\Utils\ApplicationContext;
use Hyperf\Cache\Cache as BaseCache;

/**
 *缓存工具
 * @author lhp
 */
class Cache
{
    /**
     * @var BaseCache
     */
    static $cache;
    
    protected static function getCache()
    {
        if ( ! static::$cache) {
            static::$cache = ApplicationContext::getContainer()->get(\Psr\SimpleCache\CacheInterface::class);
        }
        
        return static::$cache;
    }
    
    /**
     * 删除缓存
     * @param string $key
     * @return mixed
     */
    static function forget($key)
    {
        return static::getCache()->delete($key);
    }
    
    /**
     * 添加缓存
     * @param string $key
     * @param string $value
     * @param integer $ttl
     * @return mixed
     */
    static function put($key, $value, $ttl = null)
    {
        return static::getCache()->set($key, $value, $ttl);
    }
    
    /**
     * 获取缓存
     * @param string $key
     * @return mixed
     */
    static function get($key)
    {
        return static::getCache()->get($key);
    }
}
